package cn.iocoder.yudao.module.design.controller.admin.flowtaskaction;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.design.controller.admin.flowtaskaction.vo.*;
import cn.iocoder.yudao.module.design.convert.flowtaskaction.FlowTaskActionConvert;
import cn.iocoder.yudao.module.design.dal.dataobject.flowtaskaction.FlowTaskActionDO;
import cn.iocoder.yudao.module.design.service.flowtaskaction.FlowTaskActionService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;


@Tag(name = "管理后台 - 任务处理模型")
@RestController
@RequestMapping("/design/flow-task-action")
@Validated
public class FlowTaskActionController {

    @Resource
    private FlowTaskActionService flowTaskActionService;

    @PostMapping("/create")
    @Operation(summary ="创建任务处理模型")
    @PreAuthorize("@ss.hasPermission('design:flow-task-action:create')")
    public CommonResult<Long> createFlowTaskAction(@Valid @RequestBody FlowTaskActionCreateReqVO createReqVO) {
        return success(flowTaskActionService.createFlowTaskAction(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary ="更新任务处理模型")
    @PreAuthorize("@ss.hasPermission('design:flow-task-action:update')")
    public CommonResult<Boolean> updateFlowTaskAction(@Valid @RequestBody FlowTaskActionUpdateReqVO updateReqVO) {
        flowTaskActionService.updateFlowTaskAction(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary ="删除任务处理模型")
    @Parameter(name = "id", description = "编号", required = true )
    @PreAuthorize("@ss.hasPermission('design:flow-task-action:delete')")
    public CommonResult<Boolean> deleteFlowTaskAction(@RequestParam("id") Long id) {
        flowTaskActionService.deleteFlowTaskAction(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary ="获得任务处理模型")
    @Parameter(name = "id", description = "编号", required = true, example = "1024" )
    @PreAuthorize("@ss.hasPermission('design:flow-task-action:query')")
    public CommonResult<FlowTaskActionRespVO> getFlowTaskAction(@RequestParam("id") Long id) {
        FlowTaskActionDO flowTaskAction = flowTaskActionService.getFlowTaskAction(id);
        return success(FlowTaskActionConvert.INSTANCE.convert(flowTaskAction));
    }

    @GetMapping("/list")
    @Operation(summary ="获得任务处理模型列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048" )
    @PreAuthorize("@ss.hasPermission('design:flow-task-action:query')")
    public CommonResult<List<FlowTaskActionRespVO>> getFlowTaskActionList(@RequestParam("ids") Collection<Long> ids) {
        List<FlowTaskActionDO> list = flowTaskActionService.getFlowTaskActionList(ids);
        return success(FlowTaskActionConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary ="获得任务处理模型分页")
    @PreAuthorize("@ss.hasPermission('design:flow-task-action:query')")
    public CommonResult<PageResult<FlowTaskActionRespVO>> getFlowTaskActionPage(@Valid FlowTaskActionPageReqVO pageVO) {
        PageResult<FlowTaskActionDO> pageResult = flowTaskActionService.getFlowTaskActionPage(pageVO);
        return success(FlowTaskActionConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary ="导出任务处理模型 Excel")
    @PreAuthorize("@ss.hasPermission('design:flow-task-action:export')")
   
    public void exportFlowTaskActionExcel(@Valid FlowTaskActionExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<FlowTaskActionDO> list = flowTaskActionService.getFlowTaskActionList(exportReqVO);
        // 导出 Excel
        List<FlowTaskActionExcelVO> datas = FlowTaskActionConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "任务处理模型.xls", "数据", FlowTaskActionExcelVO.class, datas);
    }

}
